package com.genntii.examManager.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genntii.examManager.common.utils.AliOssUtil;
import com.genntii.examManager.domain.dto.UserUpdateDTO;
import com.genntii.examManager.domain.entity.User;
import com.genntii.examManager.domain.vo.UserLoginVO;
import com.genntii.examManager.mapper.UserMapper;
import com.genntii.examManager.service.UserService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Resource
    private AliOssUtil aliOssUtil;

    @Resource
    private UserMapper userMapper;

    @Override
    public void test() {
        System.out.println(userMapper.selectById(1));
    }

    @Override
    public UserLoginVO updateBasicInformation(UserUpdateDTO userUpdateDTO, Long userId) {
        userMapper.updateById(User.builder()
                .id(userId)
                .sex(userUpdateDTO.getSex())
                .birthday(userUpdateDTO.getBirthday())
                .nickname(userUpdateDTO.getNickname())
                .avatar(userUpdateDTO.getAvatar())
                .build());
        User user = userMapper.selectById(userId);
        return UserLoginVO.builder()
                .id(userId)
                .username(user.getUsername())
                .nickname(user.getNickname())
                .sex(user.getSex() == 1?"男":"女")
                .birthday(user.getBirthday())
                .createTime(user.getCreateTime())
                .role(user.getRole())
                .avatar(user.getAvatar())
                .build();

    }

    @Override
    public String upload(MultipartFile file) {
        String url;
        try {
            url = aliOssUtil.upload(file.getBytes(),file.getOriginalFilename());
        } catch (IOException e) {
            throw new RuntimeException(e+"上传失败");
        }
        return url;
    }
}
